import {getTopicPageData} from "../api/GetTopicPageData";

/**
 * 加载栏目信息数据mixin
 * @author wwy
 */
export default {
    name:"LoadTopicPageDataMixin",
    data(){
        return{
            // 分页数据
            pageData:{
                current:1,
                records:[],
                total:0,
            },
            // 配置数据
            loadPageDataSetting:{
                // 栏目code
                code:null,
                // 分页大小
                pageSize:12,
                // 是否加载中
                loading:false,
                // 是否加载失败
                fail:false,
                // 其他参数
                params:{},
            },
        }
    },

    methods:{
        //设置loadPageDataSetting的params
        setParams(params){
            this.loadPageDataSetting.params = params || {}
        },
        //设置分页大小
        setPageSize(pageSize){
            this.loadPageDataSetting.pageSize = pageSize || 10;
        },
        //设置栏目code
        setPageDataCode(code) {
            this.loadPageDataSetting.code = code;
        },
        //加载页面数据
        loadPageData(current){
            const setting = this.loadPageDataSetting || {}
            const code = setting.code
            current = current || this.pageData.current || 1
            // console.log(`loadTopicPageData code = ${code} current=${current}`)
            // 省流
            if(setting.loading){
                return
            }
            // 加载栏目数据 设置状态为加载中
            this.loadPageDataSetting.loading = true
            this.pageData.current = current
            this.pageData.records = []
            // 从后台获取栏目数据
            return getTopicPageData(code,{
                ...(this.loadPageDataSetting.params || {}),
                page:current,
                pageSize:setting.pageSize
            }).then(res => {
                // 设置后台返回数据到pageData中
                this.pageData = res.result || {current: 1}
                console.log("this.loadPageDataSetting = ", this.loadPageDataSetting);
                console.log("1234 this.pageData = ", this.pageData);
            }).catch(err => { // 加载失败
                console.error(err)
                this.loadPageDataSetting.fail = true
            }).finally(() => { // 将状态设置为加载完毕
                this.loadPageDataSetting.loading = false
            })
        },
        onLoadPageData(code, current){
            this.setPageDataCode(code);
            this.loadPageData(current);
        }

    }

}
